Use the pan manipulator to enable users to move nodes in your Kanzi application. The pan manipulator is one of the input manipulators you can use to add gesture recognition to nodes in your Kanzi application. The pan gesture tracks the position of the pointer that moves on the device screen to calculate the amount by which to move a node.
In this step of the tutorial you use the pan manipulator to enable moving the map.
The starting point of this tutorial is stored in the <KanziWorkspace>/Tutorials/Pan zoom tap/Start directory.
The <KanziWorkspace>/Tutorials/Pan zoom tap/Completed directory contains the completed project of this tutorial.
The starting point project contains the content you need to complete this tutorial:
In this section you create and use a pan manipulator to move the map when the user puts the pointer down on the map and moves the pointer.
To pan the map:
If you open the tutorial solution in Visual Studio 2017, when asked to retarget the project to the latest Microsoft toolset, click Cancel.
To open the directory of a Kanzi Studio project from Kanzi Studio, select > Open in Windows Explorer.
PanZoomTap
class, after the public section, define the handler for the PanManipulator::MovedMessage
message:private:
// Define the handler for the PanManipulator::MovedMessage
message from 2D nodes
// that have an input manipulator which generates pan messages.
// This handler translates a 2D node for the amount of the pan gesture.
void onPanMoved(PanManipulator::MovedMessageArguments& messageArguments)
{
// Get from the message arguments the node that the user pans.
Node2DSharedPtr mapNode = dynamic_pointer_cast<Node2D>(messageArguments.getSource());
// Get the distance in pixels for which the pan progressed
// since the last message in the pan gesture sequence.
Vector2 translationDelta = messageArguments.getDelta();
// Get the Render Transformation property of the Map node.
SRTValue2D mapNodeSRT = mapNode->getRenderTransformation();
// Get the current translation of the Map node.
Vector2 translation = mapNodeSRT.getTranslation();
// Apply the translation from the pan message.
Vector2 translationTarget = translation - translationDelta;
// Get the parent node of the Map node.
Node2D* containerNode = dynamic_cast<Node2D*>(mapNode->getParent());
// Get the size of the parent node.
Vector2 containerSize = containerNode->getActualSize();
// Get the Render Transformation property Scale property field of the Map node.
Vector2 mapScale = mapNodeSRT.getScale();
// Calculate the scaled size of the Map node.
Vector2 mapSizeScaled = componentWiseMultiply(mapNode->getActualSize(), mapScale);
// Do not pan outside the boundaries of the Map node.
// When calculating the boundaries, take into account the current scale of the map.
if (mapScale.getX() >= 1.0f)
{
translationTarget = componentWiseMax(componentWiseMin(translationTarget, (mapSizeScaled - containerSize) / 2), (containerSize - mapSizeScaled) / 2);
}
else if (mapScale.getX() < 1.0f)
{
translationTarget = componentWiseMin(componentWiseMax(translationTarget, (containerSize - mapSizeScaled) / 2), (mapSizeScaled - containerSize) / 2);
}
// Set the new translation.
mapNodeSRT.setTranslation(translationTarget);
// Apply the new transform to the Map node.
mapNode->setRenderTransformation(mapNodeSRT);
}
onProjectLoaded
() function create a PanManipulator
manipulator and subscribe to its messages at the Map node:virtual void onProjectLoaded() KZ_OVERRIDE { // Pointer to the domain. Domain* domain = getDomain(); // Get the Screen node. ScreenSharedPtr screen = getScreen(); // Get the Map node using its alias. Node2DSharedPtr mapNode = screen->lookupNode<Node2D>("#Map"); // Create an input manipulator that generates pan messages. PanManipulatorSharedPtr panManipulator = PanManipulator::create(domain); // Set the threshold in pixels on the horizontal and vertical axis that the pointer // must move before the input manipulator recognizes the pan gesture. panManipulator->setRecognitionThreshold(Vector2(10.0f, 10.0f)); // Add the input manipulator to the Map node. mapNode->addInputManipulator(panManipulator); // Subscribe to thePanManipulator::MovedMessage
message at the Map node. // ThePanManipulator
generates this message when the user moves the pointer on the horizontal or // vertical axis more than the recognition threshold and when the pointer moves between updates. mapNode->addMessageHandler(PanManipulator::MovedMessage, bind(&PanZoomTap::onPanMoved, this, placeholders::_1)); }
To learn more about handling user input in Kanzi, see Handling user input.
To learn more about the pan manipulator, see Using the pan manipulator.
To learn more about aliases in Kanzi, see Using aliases.